#!/bin/bash
CPATH=$(cd `dirname $0`;pwd)
TOOLS="$CPATH/../lib"
source $TOOLS/lib.sh

log_info "Start: $0 $@"
if [ $# -eq 3 ];then
    OBSSERVER_IP=$1
    PASSWD=$2
    GERRIT_BRANCH=$3
else
    log_error "Usage: $0 OBSSERVER_IP PASSWD GERRIT_BRANCH"
fi

source ./config.sh || exit 1
REPO_DIR="/srv/cache/obs/tar_scm/repo/$REPODIR"
sh $TOOLS/sshcmd.sh -c "mkdir -p $REPO_DIR;rm -f repo_pull.sh" -m "$OBSSERVER_IP" -u "root" -p "$PASSWD"
sh $TOOLS/sshscp.sh -p "$PASSWD" -s "repo_pull.sh" -d "$OBSSERVER_IP:$REPO_DIR"
sh $TOOLS/sshcmd.sh -c "cd $REPO_DIR;kill -9 `ps -ef | grep -v $$ | grep repo_pull.sh | awk '{print $2}'`;sh repo_pull.sh $MANIFEST || exit 1" -m "$OBSSERVER_IP" -u "root" -p "$PASSWD"
if [ $? -ne 0 ]; then
    log_error "repo sync failed on $REPO_DIR"
fi
